#include "staffmanagement.h"
#include "ui_staffmanagement.h"
#include "inquiresql.h"
#include "addmodifystaffinformation.h"
#include <QMessageBox>

#include <QDesktopWidget>

StaffManagement::StaffManagement(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StaffManagement)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    QHeaderView* vheaderView = ui->tableWidgetStaffInformation->verticalHeader();
    vheaderView->setHidden(true);

    m_addModifyStaffInformation = new AddModifyStaffInformation(this);
    m_addModifyStaffInformation->hide();

    connect(m_addModifyStaffInformation, SIGNAL(signalDisplayStaffInformation()),
            this, SLOT(slotDisplayStaffInformation()));
}

StaffManagement::~StaffManagement()
{
    delete ui;
}

void StaffManagement::slotDisplayStaffInformation()
{
    m_list.clear();
    m_list = m_sql->InquireStaffInformationList();
    ui->tableWidgetStaffInformation->clear();

    setQtableWidgetHeaderStyle();

    ui->tableWidgetStaffInformation->setRowCount(m_list.count());

    for(int i = 0; i < m_list.count(); ++i)
    {
        QTableWidgetItem *staffID           = new QTableWidgetItem(
                    m_list.at(i).get_personID());
        QTableWidgetItem *staffName         = new QTableWidgetItem(
                    m_list.at(i).get_personName());
        QTableWidgetItem *gender            = new QTableWidgetItem(
                    m_list.at(i).get_personGender());
        QTableWidgetItem *staffBirthday     = new QTableWidgetItem(
                    m_list.at(i).get_personBirthday());
        QTableWidgetItem *entryDate         = new QTableWidgetItem(
                    m_list.at(i).get_entryDate());
        QTableWidgetItem *staffAge          = new QTableWidgetItem(
                    m_list.at(i).get_personAge());
        QTableWidgetItem *wage              = new QTableWidgetItem(
                    m_list.at(i).get_wage());
        QTableWidgetItem *staffTel          = new QTableWidgetItem(
                    m_list.at(i).get_personTel());
        QTableWidgetItem *staffAdress       = new QTableWidgetItem(
                    m_list.at(i).get_personAdress());
        QTableWidgetItem *remark            = new QTableWidgetItem(
                    m_list.at(i).get_remark());

        //设置居中对齐
        staffID->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        staffName->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter );
        gender->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        staffBirthday->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        entryDate->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        staffAge->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        wage->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        staffTel->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter );
        staffAdress->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter );
        remark->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter );

        //设置文字颜色
//        rechargeTimes->setTextColor(QColor(0, 0, 255));
//        saleQuantity->setTextColor(QColor(0, 0, 255));
//        rechargeAmount->setTextColor(QColor(255, 0, 0));
//        totalSales->setTextColor(QColor(255, 0, 0));
//        profit->setTextColor(QColor(255, 0, 255));

        //设置文字字体大小
        staffID->setFont(QFont("DejaVu Sans", 11, 2, false));
        staffName->setFont(QFont("DejaVu Sans", 11, 2, false));
        gender->setFont(QFont("DejaVu Sans", 11, 2, false));
        staffBirthday->setFont(QFont("DejaVu Sans", 11, 2, false));
        entryDate->setFont(QFont("DejaVu Sans", 11, 2, false));
        staffAge->setFont(QFont("DejaVu Sans", 11, 2, false));
        wage->setFont(QFont("DejaVu Sans", 11, 2, false));
        staffTel->setFont(QFont("DejaVu Sans", 11, 2, false));
        staffAdress->setFont(QFont("DejaVu Sans", 11, 2, false));
        remark->setFont(QFont("DejaVu Sans", 11, 2, false));

        ui->tableWidgetStaffInformation->setItem(i, 0, staffID);
        ui->tableWidgetStaffInformation->setItem(i, 1, staffName);
        ui->tableWidgetStaffInformation->setItem(i, 2, gender);
        ui->tableWidgetStaffInformation->setItem(i, 3, staffBirthday);
        ui->tableWidgetStaffInformation->setItem(i, 4, entryDate);
        ui->tableWidgetStaffInformation->setItem(i, 5, staffAge);
        ui->tableWidgetStaffInformation->setItem(i, 6, wage);
        ui->tableWidgetStaffInformation->setItem(i, 7, staffTel);
        ui->tableWidgetStaffInformation->setItem(i, 8, staffAdress);
        ui->tableWidgetStaffInformation->setItem(i, 9, remark);
    }

    ui->tableWidgetStaffInformation->setCurrentItem(
                ui->tableWidgetStaffInformation->item(m_list.count() - 1, 0));

    //设置与文字相匹配的单元格大小
    ui->tableWidgetStaffInformation->resizeColumnsToContents();
    ui->tableWidgetStaffInformation->resizeRowsToContents();

    setTableWidgetColor();
}

void StaffManagement::setQtableWidgetHeaderStyle()
{
    QStringList headers;
    headers << tr("员工编号") << tr("员工姓名") << tr("性别") << tr("生日")
            << tr("入职日期") << tr("年龄") << tr("员工工资")
            << tr("联系电话") << tr("联系地址") << tr("备注信息");

    ui->tableWidgetStaffInformation->setColumnCount(10);
    ui->tableWidgetStaffInformation->setHorizontalHeaderLabels(headers);

    for (int i = 0; i < 10; ++i)
    {
        ui->tableWidgetStaffInformation->horizontalHeaderItem(i)->setFont(QFont("Helvetica", 12, 2, false));
        ui->tableWidgetStaffInformation->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
    }
}

void StaffManagement::setTableWidgetColor()
{
    for (int i = 0; i < ui->tableWidgetStaffInformation->rowCount(); ++i)
    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < ui->tableWidgetStaffInformation->columnCount(); ++j)
            {
                QTableWidgetItem *item = ui->tableWidgetStaffInformation->item(i,j);
                if (item)
                {
                    const QColor color = QColor(252,222,156);
                    item->setBackgroundColor(color);
                }
            }
        }
    }
}

void StaffManagement::on_pushButtonNew_clicked()
{
    m_addModifyStaffInformation->slotDisplayAddModifyStaffInformation(addStaffInformationPage, 0);
    m_addModifyStaffInformation->setWindowFlags(Qt::Dialog);
    m_addModifyStaffInformation->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
    m_addModifyStaffInformation->setAttribute(Qt::WA_ShowModal, true);
    m_addModifyStaffInformation->move((QApplication::desktop()->width() - m_addModifyStaffInformation->width())/2,
               (QApplication::desktop()->height() - m_addModifyStaffInformation->height())/2);
    m_addModifyStaffInformation->show();
}

void StaffManagement::on_pushButtonModity_clicked()
{
    int staffID = m_list.at(ui->tableWidgetStaffInformation->currentRow())
            .get_personID().toInt();
    m_addModifyStaffInformation->slotDisplayAddModifyStaffInformation(modifyStaffInformationPage, staffID);

    m_addModifyStaffInformation->setWindowFlags(Qt::Dialog);
//    m_addModifyStaffInformation->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
//    m_addModifyStaffInformation->setAttribute(Qt::WA_ShowModal, true);
    m_addModifyStaffInformation->move((QApplication::desktop()->width() - m_addModifyStaffInformation->width())/2,
               (QApplication::desktop()->height() - m_addModifyStaffInformation->height())/2);
    m_addModifyStaffInformation->show();
}

void StaffManagement::on_pushButtonDelete_clicked()
{
    switch(QMessageBox::question(this, tr("              ★温馨提示★"),
                          tr("\n您确定要删除当前的记录吗?删除之后将不可恢复"),
                          QMessageBox::Yes | QMessageBox::No ,QMessageBox::Yes))
    {
        case QMessageBox::Yes:
            m_sql->DeleteStaffInformation(m_list
                  .at(ui->tableWidgetStaffInformation->currentRow())
                  .get_personID());
            slotDisplayStaffInformation();
            break;
        case QMessageBox::No:
            break;
        default:
            break;
    }
}
